package com.example.demo.Demo1;

import com.example.demo.Character.Person;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

@RequestMapping("/Test")
@RestController
public class Test {
    @RequestMapping("/test01")
    public String fun(String[] array){
        return "接收到的参数为："+ Arrays.toString(array);

    }

    @RequestMapping("/test02")
    public String fun1(@RequestParam List<String> list){
        return "接收到的参数为："+list;
    }
    @RequestMapping("/test03")   //这个就是约定好了通过Json格式传递的
    public String fun2(@RequestBody Person person){
        return "接收到的参数为："+person.toString();
    }

    @RequestMapping("/test88")
    @ResponseBody
    public String fun44(@RequestBody Person person){
        return "person"+person;
    }
    @RequestMapping("/test04/{blogId}")
    public String fun3(@PathVariable Integer blogId){  //这里这个blogId必须跟注解的{}名字一样才行
        return "blogId"+blogId;
    }
    @RequestMapping("/getcookie")
    public String getcookie(@CookieValue String bite){  //bite为key-value结构的key；这个bite的名字必须与前端发来请求的cookie的key名字一样
        return "cookie存取的值为："+bite;  //这个bite打印的为bite对应的value
    }

    @RequestMapping("/setsession")
  public String setSession(HttpServletRequest request){
        HttpSession session = request.getSession(true);
        session.setAttribute("username","zhangsan");
        return "success";
    }
    @RequestMapping("/getSession")
    public String getSession(@SessionAttribute String username){
        return "得到的session为"+username;

    }
    @RequestMapping("/getHeader")
    public String getHeader(HttpServletRequest request){
        String Connection = request.getHeader("Connection");
        return "得到响应报文的Connection为："+Connection;
    }
@RequestMapping("/getHeader1")
    public String getHeader1(@RequestHeader("User-Agent") String userAgent){
        return "得到的响应报文的User-Agent为："+userAgent;
}
@RequestMapping("/Person")
    public Person returnJson(){
        Person person = new Person();
        person.setId(1);
        person.setName("lisi");
        person.setPhone("123456");
        return person;
}
@RequestMapping("/sum")
    public String add(Integer num1,Integer num2){
       Integer sum = num1+num2;
        return "求和后的结果为："+sum;
}

}
